/*----------------------------------------------------------------------------
* This unit test tests the salvager card effect . 
*----------------------------------------------------------------------------*/
#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <time.h>


// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 0

void checkSalvager(struct gameState *state, int handPos) {
	int check,  choice1, choice2, choice3;
	struct gameState preG;	
	memcpy (&preG, state, sizeof(struct gameState));	
	
	int p = preG.whoseTurn;

	choice1 = floor(Random() * 256);
	choice2 = floor(Random() * 256);
	choice3 = floor(Random() * 256);
       
	check = cardEffect(salvager, choice1, choice2, choice3, state, handPos, 0);  

	preG.numBuys++;

	if (choice1){
		//gain coins equal to trashed card
		preG.coins = preG.coins + getCost( preG.hand[p][choice1] );
		//trash card
		discardCard(choice1, p, &preG, 1);
	}

	//Discard card
	discardCard(handPos, p, &preG, 0);

	// Check that result is correct
	if(check != 0)
		printf("salvager card effect: FAIL. \n");	
	if(memcmp(&preG, state, sizeof(struct gameState)) != 0)
		printf("salvager card effect: FAIL. \n");	

	
}

int main() {
  	struct gameState G;
	int i, p, deckCt, discardCt, handCt, handPos, numPlayers;
  	SelectStream(3);
  	PutSeed((long)3);  	

	printf ("TESTING salvager card effect:\n");

	//randomize number of players
	numPlayers = 2 + (int)floor(Random() * (MAX_PLAYERS - 2)); 
	
	for (p = 0; p < numPlayers; p++) { 
		
		// Initialize the gameState with random bytes
		for (i = 0; i < sizeof(struct gameState); i++) {
			((char*)&G)[i] = floor(Random() * 255);
		}

		G.playedCardCount = floor(Random() * MAX_DECK);
		G.numBuys = 1;
		G.coins = 0;
		
		SelectStream(2);
		handCt = (int)floor(Random() * MAX_HAND);
		deckCt = (int)floor(Random() * MAX_DECK);
		discardCt = (int)floor(Random() * MAX_DECK);
		
		G.handCount[p] = handCt;
		G.deckCount[p] = deckCt;
		G.discardCount[p] = discardCt;
		
		memset(G.hand[p], 0, sizeof(int) * handCt);		
		memset(G.deck[p], 0, sizeof(int) * deckCt);
		memset(G.discard[p], 0, sizeof(int) * discardCt);

		// Randomize cards in deck and hand
		SelectStream(2);
		for (i=0; i < deckCt; i++)
			G.deck[p][i] = floor(Random() * (treasure_map+1));

		SelectStream(4);
		for (i=0; i < handCt; i++)
			G.hand[p][i] = floor(Random() * (treasure_map+1));						

		// Set current player and a handPos within range of handCount
		G.whoseTurn = p;
		G.numActions = 1;
		handPos = floor(Random() * (G.handCount[p]));	

		checkSalvager(&G, handPos);
						
	}
	
	printf("salvager card effect: PASS. \n");
	
	return 0;
}